home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / main.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  12KB  |  464 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Python interpreter main program */
  33.  
  34. #include "Python.h"
  35. #include "osdefs.h"
  36.  
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40.  
  41. #ifdef MS_WINDOWS
  42. #include <fcntl.h>
  43. #endif
  44.  
  45. #if defined(PYOS_OS2) || defined(MS_WINDOWS)
  46. #define PYTHONHOMEHELP "<prefix>\\lib"
  47. #else
  48. #define PYTHONHOMEHELP "<prefix>/python1.5"
  49. #endif
  50.  
  51. /* missing prototypes (IJ) */
  52. int Py_Main Py_PROTO((int argc, char **argv));
  53. void Py_GetArgcArgv Py_PROTO((int *argc, char ***argv));
  54.  
  55. /* Interface to getopt(): */
  56. extern int optind;
  57. extern char *optarg;
  58. extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
  59.  
  60.  
  61. /* For Py_GetArgcArgv(); set by main() */
  62. static char **orig_argv;
  63. static int  orig_argc;
  64.  
  65. /* Short usage message (with %s for argv0) */
  66. static char *usage_line =
  67. "usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-x] [-X] [-c cmd | file | -] [arg] ...\n";
  68.  
  69. /* Long usage message, split into parts < 512 bytes */
  70. static char *usage_top = "\
  71. Options and arguments (and corresponding environment variables):\n\
  72. -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  73. -i     : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
  74.          and force prompts, even if stdin does not appear to be a terminal\n\
  75. -O     : optimize generated bytecode (a tad)\n\
  76. -S     : don't imply 'import site' on initialization\n\
  77. -t     : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
  78. ";
  79. static char *usage_mid = "\
  80. -u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
  81. -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
  82. -x     : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
  83. -X     : disable class based built-in exceptions\n\
  84. -c cmd : program passed in as string (terminates option list)\n\
  85. file   : program read from script file\n\
  86. -      : program read from stdin (default; interactive mode if a tty)\n\
  87. ";
  88. static char *usage_bot = "\
  89. arg ...: arguments passed to program in sys.argv[1:]\n\
  90. Other environment variables:\n\
  91. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  92. PYTHONPATH   : '%c'-separated list of directories prefixed to the\n\
  93.                default module search path.  The result is sys.path.\n\
  94. PYTHONHOME   : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
  95.                The default module search path uses %s.\n\
  96. ";
  97.  
  98.  
  99. #ifdef __SASC
  100. /** Amiga SAS/C stack settings etc **/
  101. extern long __stack = 19000;
  102. extern long __STKNEED = 1000;
  103. extern char __stdiowin[] = "CON:0/12/640/200/Python1.5";
  104. extern char __stdiov37[] = "/AUTO";
  105. #endif
  106.  
  107. #ifdef _AMIGA
  108. #include <proto/dos.h>
  109. #include <proto/exec.h>
  110. #endif /* AMIGA */
  111.  
  112. #ifdef INET225
  113. #include <proto/socket.h>
  114.  
  115. struct Library *SockBase = NULL;
  116.  
  117. int checksocketlib(void)
  118. {
  119.     if(!SockBase)
  120.     {
  121.         if(SockBase=OpenLibrary("inet:libs/socket.library",4))
  122.         {
  123.             setup_sockets(FD_SETSIZE, &errno);
  124.         }
  125.         else
  126.         {
  127.             PyErr_SetString(PyExc_SystemError, "Couldn't open inet:libs/socket.library V4+ (I-Net225 started?)");
  128.             return 0;
  129.         }
  130.     }
  131.     return 1;
  132. }
  133.  
  134. #endif /* INET225 */
  135.  
  136. #ifdef AMITCP
  137. #include <proto/socket.h>
  138. #include <amitcp/socketbasetags.h>
  139.  
  140. /* proto for special AmiTCP utility funcion; see _chkufb.c */
  141. extern long _install_AmiTCP_callback(void);
  142.  
  143.  
  144. /* global h_errno */
  145. int h_errno = 0;
  146.  
  147. struct Library *UserGroupBase = NULL;
  148. struct Library *SocketBase = NULL;
  149.  
  150. int checkusergrouplib(void)
  151. {
  152.     if(!UserGroupBase)
  153.     {
  154.         if(!(UserGroupBase=OpenLibrary("usergroup.library",4)))
  155.         {
  156.             PyErr_SetString(PyExc_SystemError, "Couldn't open usergroup.library");
  157.             return 0;
  158.         }
  159.     }
  160.     return 1;
  161. }
  162.  
  163. int checksocketlib(void)
  164. {
  165.     if(!SocketBase)
  166.     {
  167.         if(SocketBase=OpenLibrary("bsdsocket.library",4))
  168.         {
  169.             /*
  170.              * Succesfull. Now tell bsdsocket.library:
  171.              * - the address of our errno
  172.              * - the address of our h_errno
  173.              * - our program name
  174.              */
  175.             SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
  176.                            SBTM_SETVAL(SBTC_HERRNOLONGPTR), &h_errno,
  177.                            SBTM_SETVAL(SBTC_LOGTAGPTR), "Python",
  178.                            TAG_END);
  179.         }
  180.         else
  181.         {
  182.             PyErr_SetString(PyExc_SystemError, "Couldn't open bsdsocket.library (start AmiTCP)");
  183.             return 0;
  184.         }
  185.     }
  186.     return 1;
  187. }
  188.  
  189. #endif
  190.  
  191. #ifdef _AMIGA
  192. BOOL from_WB = FALSE;    /* not static! getpath.c needs it! */
  193. static void AmigaCleanup(void)
  194. {
  195. #ifdef AMITCP
  196.     if(UserGroupBase)
  197.     {
  198.         CloseLibrary(UserGroupBase);
  199.         UserGroupBase=NULL;
  200.     }
  201.     if(SocketBase)
  202.     {
  203.         CloseLibrary(SocketBase);
  204.         SocketBase=NULL;
  205.     }
  206. #endif
  207. #ifdef INET225
  208.     if(SockBase)
  209.     {
  210.         cleanup_sockets();
  211.         CloseLibrary(SockBase);
  212.         SockBase = NULL;
  213.     }
  214. #endif
  215.     if(from_WB) Delay(112); // small exit delay before closing WB window
  216. }
  217. #endif
  218.  
  219.  
  220. /* Main program */
  221.  
  222. int
  223. Py_Main(argc, argv)
  224.     int argc;
  225.     char **argv;
  226. {
  227.     int c;
  228.     int sts;
  229.     char *command = NULL;
  230.     char *filename = NULL;
  231.     FILE *fp = stdin;
  232.     char *p;
  233.     int inspect = 0;
  234.     int unbuffered = 0;
  235.     int skipfirstline = 0;
  236.     int stdin_is_interactive = 0;
  237.  
  238. #ifdef _AMIGA
  239.     if(argc == 0)
  240.     {
  241.         /* Invoked from WorkBench... use WorkBench arguments  */
  242.         /* Make sure <dos.h> was included earlier in order to */
  243.         /* declare _WBArgc and _WBArgv.                       */
  244.         argc = _WBArgc;
  245.         argv = _WBArgv;
  246.         from_WB = TRUE;
  247.     }
  248.  
  249.     atexit(AmigaCleanup);   /* cleanup func */
  250. //  setbuf(stderr,NULL);    /* set error output UNBUFFERED */
  251.  
  252. #endif
  253.  
  254.     orig_argc = argc;    /* For Py_GetArgcArgv() */
  255.     orig_argv = argv;
  256.  
  257.     if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
  258.         inspect = 1;
  259.     if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
  260.         unbuffered = 1;
  261.  
  262.     while ((c = getopt(argc, argv, "c:diOStuvxX")) != EOF) {
  263.         if (c == 'c') {
  264.             /* -c is the last option; following arguments
  265.                that look like options are left for the
  266.                the command to interpret. */
  267.             command = malloc(strlen(optarg) + 2);
  268.             if (command == NULL)
  269.                 Py_FatalError(
  270.                    "not enough memory to copy -c argument");
  271.             strcpy(command, optarg);
  272.             strcat(command, "\n");
  273.             break;
  274.         }
  275.         
  276.         switch (c) {
  277.  
  278.         case 'd':
  279.             Py_DebugFlag++;
  280.             break;
  281.  
  282.         case 'i':
  283.             inspect++;
  284.             Py_InteractiveFlag++;
  285.             break;
  286.  
  287.         case 'O':
  288.             Py_OptimizeFlag++;
  289.             break;
  290.  
  291.         case 'S':
  292.             Py_NoSiteFlag++;
  293.             break;
  294.  
  295.         case 't':
  296.             Py_TabcheckFlag++;
  297.             break;
  298.  
  299.         case 'u':
  300.             unbuffered++;
  301.             break;
  302.  
  303.         case 'v':
  304.             Py_VerboseFlag++;
  305.             break;
  306.  
  307.         case 'x':
  308.             skipfirstline = 1;
  309.             break;
  310.  
  311.         case 'X':
  312.             Py_UseClassExceptionsFlag = 0;
  313.             break;
  314.  
  315.         /* This space reserved for other options */
  316.  
  317.         default:
  318.             fprintf(stderr, usage_line, argv[0]);
  319.             fprintf(stderr, usage_top);
  320.             fprintf(stderr, usage_mid);
  321.             fprintf(stderr, usage_bot,
  322.                 DELIM, DELIM, PYTHONHOMEHELP);
  323. #ifdef _AMIGA
  324.             fprintf(stderr,"Python and Python programs can also be started from the Workbench,\ntooltypes will be converted to command-line arguments.\n");
  325. #endif
  326.             exit(2);
  327.             /*NOTREACHED*/
  328.  
  329.         }
  330.     }
  331.  
  332.     if (command == NULL && optind < argc &&
  333.         strcmp(argv[optind], "-") != 0)
  334.     {
  335.         filename = argv[optind];
  336.         if (filename != NULL) {
  337.             if ((fp = fopen(filename, "r")) == NULL) {
  338.                 fprintf(stderr, "%s: can't open file '%s'\n",
  339.                     argv[0], filename);
  340.                 exit(2);
  341.             }
  342.             else if (skipfirstline) {
  343.                 char line[256];
  344.                 fgets(line, sizeof line, fp);
  345.             }
  346.         }
  347.     }
  348.  
  349.     stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
  350.  
  351.     if (unbuffered) {
  352. #ifdef MS_WINDOWS
  353.         _setmode(fileno(stdin), O_BINARY);
  354.         _setmode(fileno(stdout), O_BINARY);
  355. #endif
  356. #ifndef MPW
  357. #ifdef HAVE_SETVBUF
  358.         setvbuf(stdin,  (char *)NULL, _IONBF, BUFSIZ);
  359.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  360.         setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
  361. #else /* !HAVE_SETVBUF */
  362.         setbuf(stdin,  (char *)NULL);
  363.         setbuf(stdout, (char *)NULL);
  364.         setbuf(stderr, (char *)NULL);
  365. #endif /* !HAVE_SETVBUF */
  366. #else /* MPW */
  367.         /* On MPW (3.2) unbuffered seems to hang */
  368.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  369.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  370.         setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
  371. #endif /* MPW */
  372.     }
  373.     else if (Py_InteractiveFlag) {
  374. #ifdef MS_WINDOWS
  375.         /* Doesn't have to have line-buffered -- use unbuffered */
  376.         setvbuf(stdin,  (char *)NULL, _IONBF, BUFSIZ);
  377.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  378. #else /* !MS_WINDOWS */
  379. #ifdef HAVE_SETVBUF
  380.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  381.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  382. #endif /* HAVE_SETVBUF */
  383. #endif /* !MS_WINDOWS */
  384.         /* Leave stderr alone - it should be unbuffered anyway. */
  385.       }
  386.  
  387.     Py_SetProgramName(argv[0]);
  388.     Py_Initialize();
  389.  
  390. #ifdef AMITCP
  391.     /** Sadly, this function cannot be called as a SAS/C standard **/
  392.     /** constructor function. It needs to have the interpreter **/
  393.     /** initialised because it uses the Python Error functions... **/
  394.     (void)_install_AmiTCP_callback();
  395. #endif
  396.  
  397.     if (Py_VerboseFlag ||
  398.         (command == NULL && filename == NULL && stdin_is_interactive))
  399.         fprintf(stderr, "Python %s on %s\n%s\n",
  400.             Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
  401.     
  402.     
  403.     if (command != NULL) {
  404.         /* Backup optind and force sys.argv[0] = '-c' */
  405.         optind--;
  406.         argv[optind] = "-c";
  407.     }
  408.  
  409.     PySys_SetArgv(argc-optind, argv+optind);
  410.  
  411.     if ((inspect || (command == NULL && filename == NULL)) &&
  412.         isatty(fileno(stdin))) {
  413.         PyObject *v;
  414.         v = PyImport_ImportModule("readline");
  415.         if (v == NULL)
  416.             PyErr_Clear();
  417.         else
  418.             Py_DECREF(v);
  419.     }
  420.  
  421.     if (command) {
  422.         sts = PyRun_SimpleString(command) != 0;
  423.         free(command);
  424.     }
  425.     else {
  426.         if (filename == NULL && stdin_is_interactive) {
  427.             char *startup = getenv("PYTHONSTARTUP");
  428.             if (startup != NULL && startup[0] != '\0') {
  429.                 FILE *fp = fopen(startup, "r");
  430.                 if (fp != NULL) {
  431.                     (void) PyRun_SimpleFile(fp, startup);
  432.                     PyErr_Clear();
  433.                     fclose(fp);
  434.                 }
  435.             }
  436.         }
  437.         sts = PyRun_AnyFile(
  438.             fp,
  439.             filename == NULL ? "<stdin>" : filename) != 0;
  440.         if (filename != NULL)
  441.             fclose(fp);
  442.     }
  443.  
  444.     if (inspect && stdin_is_interactive &&
  445.         (filename != NULL || command != NULL))
  446.         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  447.  
  448.     Py_Finalize();
  449.     return sts;
  450. }
  451.  
  452.  
  453. /* Make the *original* argc/argv available to other modules.
  454.    This is rare, but it is needed by the secureware extension. */
  455.  
  456. void
  457. Py_GetArgcArgv(argc, argv)
  458.     int *argc;
  459.     char ***argv;
  460. {
  461.     *argc = orig_argc;
  462.     *argv = orig_argv;
  463. }
  464.